package com.xmy.cultivate.mapper;

import com.xmy.cultivate.entity.CourseSchedulingAlways;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.PracticeTask;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;

/**
 * <p>
 * 日期总排课表，计算缺课使用 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2023-07-28
 */
public interface CourseSchedulingAlwaysMapper extends BaseMapper<CourseSchedulingAlways> {
    /**
     * 根据日期获取课次
     * @param nowDate
     * @param gradeSection
     * @return
     */
    @Select("select * from course_scheduling_always where grade_section = ${gradeSection} and #{nowDate} BETWEEN start_date and end_date and deleted =0 LIMIT 1")
    CourseSchedulingAlways getOneForDate(@Param("nowDate") LocalDate nowDate, @Param("gradeSection") Integer gradeSection);


    /**
     * 获取当前课次
     * @param yearPart
     * @param quarterNum
     * @param dateNow
     * @return
     */
    @Select({"<script>",
            "SELECT",
                    "* ",
                    "FROM",
                    "course_scheduling_always ",
                    "WHERE",
                    "( deleted = 0 AND year_part = ${yearPart} AND quarter_num = ${quarterNum} AND '${dateNow}' BETWEEN start_date AND end_date ) ",
                    "OR (",
                    "num IN ( SELECT MAX( num ) FROM course_scheduling_always WHERE year_part = ${yearPart} AND quarter_num = ${quarterNum} AND deleted = 0 )) ",
                    "and grade_section = 1",
                    "ORDER BY",
                    "num ASC ",
                    "LIMIT 1",
            "</script>"})
    public CourseSchedulingAlways getLessonNumNow(@Param("yearPart") Integer yearPart,
                                           @Param("quarterNum") Integer quarterNum,
                                           @Param("dateNow") String dateNow);
}
